/* 
 @description: 资讯JS

 @author: 韦淇霰

 @update: 2021年9月5日21:55:35
 */
 var token=localStorage.getItem('mytoken')
    const add= document.getElementById('add'),
    input =document.querySelector('input'),
    back = document.querySelector('.back'),
    submit = document.getElementById('submit'),
    closes = document.getElementById('closes'),
    search = document.querySelector('.Search'),
    title = document.getElementById('title'),
    content = document.getElementById('content'),
    auder = document.getElementById('auder')
    table = document.getElementById('tab');
    let isEdit = 0;
    const fwb = document.querySelector('.w-e-text');
    getList();
        const sou = document.querySelector('#sou');
    //搜索
    function getList(keyname='') {
        $.ajax({
        url: `http://127.0.0.1:7001/api/realtimeInfo?keyname=${keyname}`,
        method: "GET",
        headers:{
            token
        },
        success: function(res) {
            console.log(res);
            localStorage.setItem('zxtotal',res.count);
            console.log(zxtotal);
                for (var i = 0; i < res.data.length; i++) {
                    const tr = document.createElement('tr');
                    for (let data in res.data[i]) {
                        const td = document.createElement("td");
                        td.innerHTML = res.data[i][data]
                        tr.appendChild(td);
                        if(data === 'time_d'){
                            let time_d = res.data[i].time_d;
                            var times = new Date(parseInt(time_d)).toLocaleString();
                            td.innerHTML=times;
                  
                        }
                    }
                    const lasttd = document.createElement('td');
                    lasttd.innerHTML  = `<button type='button'  name="updata" class="up"  id='${res.data[i].id}'><i class="fa fa-wrench" aria-hidden="true"></i> 修改</button>
                    <button type='button' class="btn" name="delete" id='${res.data[i].id}'><i class="fa fa-trash" aria-hidden="true"></i> 删除</button>`
                    tr.appendChild(lasttd)
                    tr.id = res.data[i].id;
                    table.appendChild(tr);
                  
                }}
        }
    )}
    //添加 　
    function addLwl(data){
        $.ajax({
        url:`http://127.0.0.1:7001/api/realtimeInfo`,
        method:"post",
        data,
        success:function(res){
            if(res.code === 0){
                window.location.reload();
                closeBack();  
            }else{
                alert(res.msg);
            }
        }
    })
    }
    //删除
    function DelCa(id){
        $.ajax({
        url:`http://127.0.0.1:7001/api/realtimeInfo/${id}`,
        method:"delete",
        data:{
            id
        },
        success:function(res){
            window.location.reload();
        },
        error:()=>{
            console.log(1);
        }
    })
    }
    //编辑
    function EditCa(title,content,release_z,id){
        $.ajax({
        url:`http://127.0.0.1:7001/api/realtimeInfo/${id}`,
        method:"put",
        data:{
            title,
            content,
            release_z,
            id
        },
        headers:{
            token
        },
        success:function(res){
            if(res.code === 0){
            //    getList();
            window.location.reload();
            closeBack();  
            }else{
                alert(res.msg);
            }
        }
    })
    }
    //获取详情
    function getInfo(id){
        $.ajax({
        url:`http://127.0.0.1:7001/api/realtimeInfo/${id}`,
        method:"get",
        headers:{
            token
        },
        data:{
            id,
        },
        success: function(res) {
           const fwb = document.querySelector('.w-e-text');
           console.log(fwb);
            if(res.code === 0){
                title.value = res.data.title;
                fwb.innerHTML = res.data.content;
                auder.value = res.data.release_z;
            }else{
                alert(res.msg)
            }
            console.log(res);
        }
        
        })
    }
    let ida=0;
    //通过事件委托操作 删除和编辑
    table.onclick = function(e){
        if(e.target.className === 'btn'){
            const flag = confirm('确定删除吗');
            if(flag==true){
                DelCa(e.target.id);
            }
        }else if(e.target.className === 'up'){
            back.style.display = 'block';
            const id  = e.target.id;
               ida = id;
            getInfo(ida);
        }
    }
    const ul =document.querySelector('.ulTag');
    //搜索按钮
    search.onclick = function(){
        ul.innerHTML='' 
        getList(sou.value);
        console.log(sou.value);
        table.innerHTML = `<tr class="tab_a">
                                <th>ID</th>
                                <th>标题</th>
                                <th>内容</th>
                                <th>发布时间</th>
                                <th>发布作者</th>
                                <th>操作</th>
                            </tr>`
        function ss(keyname=''){
            $.ajax({
                url: `http://127.0.0.1:7001/api/realtimeInfo?keyname=${keyname}`,
                method: "get",
                headers:{
                    token
                    },
                success: function(res) {
                    console.log(res);
                    const ul =document.querySelector('.ulTag');
                    const nums = Math.ceil(res.count / 5);
                    console.log(nums);
                    for(let i=0;i<nums;i++){
                        const li = document.createElement('li');
                        ul.appendChild(li);
                        li.innerHTML = `<button type="button" class="dianji">${i+1}</button>`
                        
                    } 
                    const dianji = document.querySelectorAll('.dianji');
                    for(const i of dianji){
                        i.onclick = function(){
                            // ul.innerHTML=''
                            getP(i.innerHTML);
                        }
                    }
                }
            })
            }
        ss(sou.value);
    }
    //添加按钮
    add.onclick =function(){
        back.style.display = 'block';
        ida = 0;
    }
    //取消按钮
    closes.onclick = function(){
        closeBack();
    }

    function closeBack(){
        title.value = '';
        content.text ='';
        auder.value ='';
        back.style.display = 'none';
    }
    //添加与编辑提交按钮
    submit.onclick = function(){
        // const a = editor.txt.text();
        const a = editor.txt.html();
        console.log(a);
        content.text = a;
        if(ida){
            EditCa(
                title.value,
                a,
                auder.value ,
                ida,
        )
        }else {
            addLwl({
                title : title.value,
                content : a,
                release_z : auder.value
            })
        }

    }
      //富文本
      const E = window.wangEditor;
      const editor = new E('#content');
      editor.create();
      const audors = localStorage.getItem('ador_name');
      console.log(audors);
    //   $('#auder').html(audors);
      $("#auder").val(audors).text();

     //分页
     function getPage(page) {
        $.ajax({
        url: `http://127.0.0.1:7001/api/realtimeInfo?page=${page}`,
        method: "get",
        headers:{
            token
          },
        success: function(res) {
            // console.log(res);
           
            const ul =document.querySelector('.ulTag');
            const tab=document.querySelector('#tab');
            tab.innerHTML='';
            ul.innerHTML='';
            const tr=document.createElement('tr')
            table.innerHTML =  `<tr class="tab_a">
                                    <th>ID</th>
                                    <th>标题</th>
                                    <th>内容</th>
                                    <th>发布时间</th>
                                    <th>发布作者</th>
                                    <th>操作</th>
                                </tr>`
            table.appendChild(tr);
            for (var i = 0; i < res.data.length; i++) {
                const tr = document.createElement('tr');
                for (let data in res.data[i]) {
                    const td = document.createElement("td");
                    td.innerHTML = res.data[i][data]
                    tr.appendChild(td);
                    // console.log(data);
                    if(data === 'time_d'){
                        // console.log(11111);
                        let time_d = res.data[i].time_d;
                        var times = new Date(parseInt(time_d)).toLocaleString();
                        td.innerHTML=times;
                        console.log(times);
                    }
                }
              
            }
            for(const item of res.data){
                // console.log(item);
                const tr=document.createElement('tr');
                tr.innerHTML=`<td>${item.id}</td>
                    <td>${item.title}</td>
                    <td>${item.content}</td>
                    <td>${times}</td>
                    <td>${item.release_z}</td>
                    <td><button type='button' name="updata" class="up"  id='${item.id}'><i class="fa fa-wrench" aria-hidden="true"></i> 修改</button>
                    <button type='button' class="btn" name="delete" id='${item.id}'><i class="fa fa-trash" aria-hidden="true"></i> 删除</button></td>
               `
                table.appendChild(tr)
              
            }
            getInfor(page)
    
        },
        error:()=>{
           alert('没有找到id');
        }
    }
    )}
    function getInfor(page=1){
   
    $.ajax({
        url: `http://127.0.0.1:7001/api/realtimeInfo?page=${page}`,
        method: "get",
        headers:{
            token
          },
        success: function(res) {
            const ul =document.querySelector('.ulTag');
            const num = Math.ceil(res.count / 4);
            console.log(num);
            
            for(let i=0;i<num;i++){
                const li = document.createElement('li');
                ul.appendChild(li);
                li.innerHTML = `<button type="button" class="dianji">${i+1}</button>`
            } 
            const dj = document.querySelectorAll('.dianji');
            for(const key of dj){
                key.onclick = function(){
                    getPage(key.innerHTML);
                    // window.location.reload();
                }
            }
        }
    })
    }
    getInfor();